// 本文件实现LpPacketFragmentId组件
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/3/23 20:55
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const encoding = require('../encoding')
const {TlvComponentBase, InitialAble} = require('./TlvComponentBase')
var aggregation = require('../common/aggregation')//实现了多继承

class LpPacketFragmentId extends aggregation(TlvComponentBase,InitialAble,encoding.SelfEncodingBase){
    constructor() {
        super();
        this.id = BigInt(0) // uint64
    }

    getId(){
        return this.id
    }

    setId(id){
        this.id = BigInt(id)
        this.doInitial()
    }

    /**
     * @description 将 LpPacketFragmentId 线速编码为一个 TLV
     * @param enc
     * @returns {number}
     */
    wireEncode(enc){
        let totalLength = 0
        try{
            // 编码TLV-VALUE
            let tmpLen = enc.prependNonNegativeInteger(this.id)
            // DONE 错误处理
            totalLength += tmpLen

            // 编码TLV-LENGTH
            tmpLen = enc.prependVarNumber((totalLength))
            // DONE 错误处理
            totalLength += tmpLen

            // 编码 TLV-TYPE
            tmpLen = enc.prependVarNumber((encoding.tlv.ComponentType.TlvLpPacketFragmentId))
            // DONE 错误处理
            totalLength += tmpLen

            return totalLength
        }catch (e){
            throw e
        }

    }

    /**
     * @description 从 TLV Block 中解码出一个 LpPacketFragmentId
     * @param block
     */
    wireDecode(block){
        // 检查 Type 是否正确
        // getType => vlint
        try {
            if(encoding.tlv.expectType(block.getType(), (encoding.tlv.ComponentType.TlvLpPacketFragmentId))){
                // 读取Id
                let value = encoding.tlv.readNonNegativeInteger(block.getValue(), 0, Number(block.getLength()))
                this.setId(BigInt(value))
            }
        }catch (e){
            throw e
        }
    }
}

module.exports = {
    LpPacketFragmentId:LpPacketFragmentId
}
